<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="quick-filters">
  <div class="quick-filters__row">
    <form class="quick-filters__selectors" *ngIf="quickFiltersForm" [formGroup]="quickFiltersForm">
      <mat-form-field class="quick-filters__period">
        <mat-label>Period</mat-label>
        <mat-select formControlName="period" aria-label="Period selection">
          <mat-option *ngFor="let period of periods" [value]="period">{{ period.label }}</mat-option>
        </mat-select>
      </mat-form-field>

      <mat-form-field class="quick-filters__methods">
        <mat-label>HTTP methods</mat-label>
        <mat-select formControlName="methods" aria-label="Methods selection" [multiple]="true">
          <mat-option *ngFor="let httpMethod of httpMethods" [value]="httpMethod">{{ httpMethod }}</mat-option>
        </mat-select>
      </mat-form-field>

      <mat-form-field class="quick-filters__plans">
        <mat-label>Plan</mat-label>
        <mat-select formControlName="plans" [multiple]="true">
          <mat-option *ngFor="let plan of plans" [value]="plan.id">{{ plan.name }}</mat-option>
        </mat-select>
      </mat-form-field>

      <div class="action">
        <api-runtime-logs-more-filters
          [showMoreFilters]="showMoreFilters"
          [formValues]="moreFiltersValues"
          (closeMoreFiltersEvent)="this.showMoreFilters = false"
          (applyMoreFiltersEvent)="applyMoreFilters($event)"
        ></api-runtime-logs-more-filters>
        <button mat-stroked-button type="button" class="action__button" [disabled]="loading" (click)="showMoreFilters = true">
          <mat-icon svgIcon="gio:filter"></mat-icon>More
        </button>
      </div>
      <div class="action">
        <button
          mat-stroked-button
          class="action__button"
          [attr.aria-label]="'Refresh'"
          (click)="refresh.emit()"
          [disabled]="loading"
          data-testId="refresh-button"
        >
          <mat-icon svgIcon="gio:refresh-cw"></mat-icon>Refresh
        </button>
      </div>
    </form>
  </div>

  <div class="quick-filters__applied">
    <ng-container *ngIf="isFiltering">
      <mat-chip-listbox class="quick-filters__chip_list">
        <span>Filters applied:</span>
        <ng-container *ngFor="let filter of currentFilters | keyvalue">
          <mat-chip-option
            class="gio-badge-neutral quick-filters__chip"
            *ngIf="filter.value && defaultFilters[filter.key] !== filter.value"
            [removable]="true"
            (removed)="removeFilter($any(filter))"
            [matTooltip]="$any(filter.value) | chipValue"
            [disabled]="loading"
          >
            <span class="quick-filters__chip__key">{{ filter.key }}:</span>
            <span class="quick-filters__chip__value" *ngIf="!['from', 'to'].includes(filter.key); else dateBlock">
              {{ $any(filter.value) | chipValue }}
            </span>
            <ng-template #dateBlock>
              <span class="quick-filters__chip__value">{{ $any(filter.value) | date: 'yyyy-MM-dd HH:mm:ss' }}</span>
            </ng-template>
            <mat-icon matChipRemove>cancel</mat-icon>
          </mat-chip-option>
        </ng-container>
        <button mat-button class="quick-filters__reset" data-testId="reset-filters-button" (click)="resetAllFilters()">
          <mat-icon svgIcon="gio:x-circle"></mat-icon>
          <span class="quick-filters__reset__label">Reset filters</span>
        </button>
      </mat-chip-listbox>
    </ng-container>
    <ng-container *ngIf="!isFiltering"><span>No filter applied</span></ng-container>
  </div>
</div>
